package memberdao

import (
	"yundian/internel/app/helper/resphelper/memberresp"
	"yundian/internel/app/models/membermodel"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

// Get Member Balance Log By TradeNo ==== pcl
func (d *MemberDao) GetMemberPclBalanceLogByTradeNo(tradeNo string) *dingo.ErrorWrap {
	var lst    []*memberresp.MemberPclBalanceRes
	db := global.Orm.Table("member_pcl_balance").Where("trade_no = ?", tradeNo).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

func (d *MemberDao) GetMemberPclBalanceLogInfoByTradeNo(tradeNo string, memberId int) *dingo.ErrorWrap {
	lst := &membermodel.MemberPclBalance{}
	db := global.Orm.Table("member_pcl_balance").Where("trade_no = ?", tradeNo).Where("member_id = ?", memberId).Find(lst)
	return dingo.ErrorIf(db.Error, lst)
}

func (d *MemberDao) GetMemberPclBalanceLogInfoByRTradeNo(tradeNo string, memberId int) *dingo.ErrorWrap {
	lst := &membermodel.MemberPclBalance{}
	db := global.Orm.Table("member_pcl_balance").Where("trade_no = ?", "R" + tradeNo).Where("member_id = ?", memberId).Find(lst)
	return dingo.ErrorIf(db.Error, lst)
}

// Get Member Balance Log By MemberId ==== pcl
func (d *MemberDao) GetMemberPclBalanceLogByMemberId(memberId int, tradeNo string) *dingo.ErrorWrap {
	var lst    []*memberresp.MemberPclBalanceRes
	db := global.Orm.Table("member_pcl_balance").
		Where("member_id = ?", memberId).Where("trade_no = ? or trade_no = ?", tradeNo, "R"+tradeNo).Find(&lst)
	return dingo.ErrorIf(db.Error, lst)
}

// SUM Member Balance By MemberId ==== pcl
func (d *MemberDao) SumMemberPclBalanceByMemberId(memberId int) *dingo.ErrorWrap {
	var total int64
	sql := "SELECT IFNULL(SUM(amount),0) as total FROM member_pcl_balance where member_id = ?"
	db := global.Orm.Raw(sql, memberId).Find(&total)
	return dingo.ErrorIf(db.Error, total)
}

// SUM Member Order By TradeNo ==== pcl
func (d *MemberDao) SumMemberOrderByTradeNo(tradeNo string) *dingo.ErrorWrap {
	var total int64
	sql := "SELECT IFNULL(SUM(amount),0) as total FROM member_pcl_balance where trade_no = ?"
	db := global.Orm.Raw(sql, tradeNo).Find(&total)
	return dingo.ErrorIf(db.Error, total)
}

